//
//  YYPVisitorView.swift
//  YYPWeibo
//
//  Created by youyongpeng on 15/11/21.
//  Copyright © 2015年 youyongpeng. All rights reserved.
//

import UIKit
import SnapKit

class YYPVisitorView: UIView {
    
    
    // MARK: - 设置视图信息
    func setupInfo(title: String, iconImage: String?) {
    
        // 设置文字
        messageLbl.text = title
        
        // 如果图片是nil 直接返回
        guard let image = iconImage else {
        
            // 播放动画
            startAnim()
            
            // 返回
            return
        }
        
        // 设置图片
        iconView.image = UIImage(named: image)
        
        // 小房子消失
        homeIconView.hidden = true
        
        // 让遮罩移动到最底下
        sendSubviewToBack(maskIconView)
        
    }
    
    // 动画
    private func startAnim() {
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        // 细节
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20

        
        // 
        anim.removedOnCompletion = false
        
        // 添加到控件上
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    

    // 构造函数  控制器是通过纯代码开发的接口
    override init(frame: CGRect) {
        super.init(frame: frame)
     
        // 设置界面
        setupUI()
    }
    
    // 底下是 控制器通过 sb 或者 xib 创建的接口
    // error 必须注释掉,要不程序会崩溃
    required init?(coder aDecoder: NSCoder) {
    //        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        // 设置界面
        setupUI()
    }
    
    
    // MARK: - 懒加载控件
    // --> 图标
    private lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")

    // --> 遮罩
    private lazy var maskIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")

    // --> 小房子
    private lazy var homeIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")

    // --> 文字
    private lazy var messageLbl: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜")
    
    // --> 注册按钮
    lazy var registerButton: UIButton = UIButton(title: "注册", imageName: "common_button_white_disable", color:UIColor.orangeColor())
    

    // --> 注册按钮
    lazy var loginButton: UIButton = UIButton(title: "登录", imageName: "common_button_white_disable", color: UIColor.orangeColor())
    
}


/// MARK - 设置界面
extension YYPVisitorView {
    
    private func setupUI() {
    
        // 添加到视图上
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLbl)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 设置自动布局
        
        // 让每个子控件都遵循 translatesAutoresizingMaskIntoConstraints
        for v in subviews {
        
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 设置图标
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-36)
        }
       // 设置小房子
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
       // 设置文字
        messageLbl.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.centerX.equalTo(iconView.snp_centerX)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
        // 设置注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLbl.snp_left)
            make.top.equalTo(messageLbl.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        // 设置登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLbl.snp_right)
            make.top.equalTo(messageLbl.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        // 设置遮罩
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(registerButton.snp_left)
            make.right.equalTo(loginButton.snp_right)
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(loginButton.snp_bottom).offset(-36)
        }
        // 设置背景图片
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
    
}
